package com.trendit.classicpos;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.trendit.classicpos.core.KeyManager;
import com.trendit.classicpos.view.VirtualPosScreenView;

/**
 * @author zzx
 */
public class MainActivity extends Activity {

    private static VirtualPosScreenView screenView;
    private static final int MAX_WIDTH = 320;
    private static final int MAX_HEIGHT = 240;

    private void startPosProcess() {
        new Thread(() -> PosNative.getInstance().startPosProcess()).start();
    }

    public static VirtualPosScreenView getScreenView() {
        return screenView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        LinearLayout screenViewGroup = findViewById(R.id.lin_screen_view);

        screenView = new VirtualPosScreenView(this, 1, MAX_WIDTH, MAX_HEIGHT);
        screenView.setLayoutParams(new ViewGroup.LayoutParams(MAX_WIDTH, MAX_HEIGHT));
        screenViewGroup.addView(screenView);
        startPosProcess();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        int key = 0;

        switch (keyCode) {
            case KeyEvent.KEYCODE_0:
                key = KeyManager.KEY_DIGITAL0;
                break;
            case KeyEvent.KEYCODE_1:
                key = KeyManager.KEY_DIGITAL1;
                break;
            case KeyEvent.KEYCODE_2:
                key = KeyManager.KEY_DIGITAL2;
                break;
            case KeyEvent.KEYCODE_3:
                key = KeyManager.KEY_DIGITAL3;
                break;
            case KeyEvent.KEYCODE_4:
                key = KeyManager.KEY_DIGITAL4;
                break;
            case KeyEvent.KEYCODE_5:
                key = KeyManager.KEY_DIGITAL5;
                break;
            case KeyEvent.KEYCODE_6:
                key = KeyManager.KEY_DIGITAL6;
                break;
            case KeyEvent.KEYCODE_7:
                key = KeyManager.KEY_DIGITAL7;
                break;
            case KeyEvent.KEYCODE_8:
                key = KeyManager.KEY_DIGITAL8;
                break;
            case KeyEvent.KEYCODE_9:
                key = KeyManager.KEY_DIGITAL9;
                break;
            case KeyEvent.KEYCODE_FUNCTION:
                key = KeyManager.KEY_FUNCTION;
                break;
            case KeyEvent.KEYCODE_BACK:
                key = KeyManager.KEY_ESC;
                break;
            case KeyEvent.KEYCODE_DEL:
                key = KeyManager.KEY_CLEAR;
                break;
            case KeyEvent.KEYCODE_ENTER:
                key = KeyManager.KEY_ENTER;
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                key = KeyManager.KEY_UP_OR_10;
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                key = KeyManager.KEY_DOWN_OR_11;
                break;
            default:
                break;
        }
        if (key != 0) {
            KeyManager.getsInstance().push(key);
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

}
